<?php
namespace Posts\Controller;

use Common\Controller\AdminbaseController;

class PostsAdminController extends AdminbaseController {
    protected $posts_obj;

    public function _initialize() {
        parent::_initialize();
        $this->posts_obj=M('posts','cmf_');
    }
    public function index()
    {
        $obj = D('posts');
        $id = I('post.uid');
        $keyword = I('post.keyword');
        if(isset($keyword) && $keyword != ''){
            $where = "(post_name like '%{$keyword}%' or
                post_school  like '%{$keyword}%' or
                post_source  like '%{$keyword}%' or
                post_content like '%{$keyword}%' or
                post_title   like '%{$keyword}%') and";
        }
        if(isset($id) && $id != ''){
            $where .= " id = '{$id}' and";
        }
        $where .= " post_exist = 1";
        $count= $obj -> where($where)->count();
        $page = $this->page($count, 12);
        $list = $obj ->order("id asc") -> where($where) ->limit(
            $page->firstRow . ',' . $page->listRows) ->select();
        $this->assign('list', $list);
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }
    // public function index()
    // {
    //  //    $id = I('post.uid');
    //  //    $keyword = I('post.keyword');
    //  //    echo $data['id'];

    //  // $count= $this->posts_obj-> where("id like 1") -> count();



    //  // $count=$this->posts_obj->count();
    //  // $page = $this->page($count, 12);

    //  // $uid = I('post.uid');
    //  //    // $keyword = isset(I('post.keyword')) ? I('post.keyword') : '';
    //  //    echo $uid;
    //  //    echo $keyword;

    //  $count= $this->posts_obj->count();
    //  $page = $this->page($count, 10);

    //  $list = $this->posts_obj
    //  ->order("id asc")
        // ->limit($page->firstRow . ',' . $page->listRows)
    //  ->select();
    //  // var_dump($list);
    //  $this->assign('list', $list);
    //  $this->assign("page", $page->show('Admin'));

    //     $this->display();
    // }

    public function delete(){

        if(isset($_GET['id']))
            $id=intval(I('get.id'));
        $data['post_exist'] = 0;
        if($this->posts_obj->where('id='.$id) ->where(" and post_exist =1") ->save($data)){
            $this->success('删除成功',U('PostsAdmin/index'));
        }else{
            $this->error('删除失败');
        }
    }

    public function detail(){
        if(isset($_GET['id']))
            $id=intval(I('get.id'));
        $data=$this->posts_obj-> where('id='.$id)->select();
        $this->assign('data',$data);
        $this->display();
    }

    public function edit(){
        $button = I('post.button');
        $id = I('get.id');

        $obj = D('posts');
        // if(isset($_POST)){echo "post";}
        // dump($_POST);
        // if(isset($_GET)){echo "<br/>get";}
        // dump($_GET);exit();
        //结果提交（编辑或添加）
        if($button == 'submit'){
            echo "结果提交";
            if($obj -> create() ->add()){
                $this -> success('修改成功',U('PostsAdmin/index'),3);
            }else{
                $this -> error('发生错误，<br>请重试');
            }
        }
        else{
            //编辑
            if(isset($id) && $id != ''){
                $this -> assign('function','编辑'); //输出当前页面为编辑页面
                $obj = D('posts');
                $data = $obj ->where(" post_exist =1 and ") -> where('id='.$id) -> find();
                $path = $data['post_img'] ;
                $this -> assign('data',$data); //输出编辑前的信息
                $this -> display();
            }
            //添加
            else {
                $this -> assign('function','添加');
                $this -> display();
            }

        }
        // //编辑
        // if(isset($id) && $id != ''){
        //     $this -> assign('function','编辑'); //输出当前页面为编辑页面
        //     $obj = D('posts');
        //     $data = $obj ->where(" post_exist =1 and ") -> where('id='.$id) -> find();
        //     $path = $data['post_img'] ;
        //     // = U(SITE_PATH.$data['post_img']);
        //     // echo $path.'<br>';echo SITE_PATH.'<br>';
        //     // $site = U(SITE_PATH);
        //     // echo $site;
        //     $this -> assign('data',$data); //输出编辑前的信息
        //     dump($data);
        //     $this -> display();
        // }
        // //添加
        // else {
        //     $this -> assign('function','添加');
        //     // echo "添加";
        //     $this -> display();
        // }
    }

    // public function edit(){
    //     // if(isset($_GET['id']))
    //     //     $id=intval(I('get.id'));
    //     // $data=$this->posts_obj->where('id='.$id)->select();
    //     $id = I('get.id');
    //     if($id != ''){
    //         echo "编辑";
    //         $obj = D('posts');
    //         $result = $obj -> where('id='.$id) -> find();
    //         $this -> assign('result',$result);
    //     foreach ($data as $key => $value) {

    //         if($value['post_status'] == 1)
    //             $data[$key]['post_status']='已审核';
    //         else if($value['post_status'] == 0){
    //              $data[$key]['post_status']='未审核';
    //         }else{
    //             $data[$key]['post_status']='删除';
    //         }

    //         if($value['comment_status'] == 0){
    //              $data[$key]['comment_status']='未允许';
    //         }else{
    //             $data[$key]['comment_status']='允许';
    //         }

    //         if($value['istop'] == 0){
    //              $data[$key]['istop']='不置顶';
    //         }else{
    //             $data[$key]['istop']='置顶';
    //         }

    //         if($value['recommended'] == 0){
    //              $data[$key]['recommended']='不推荐';
    //         }else{
    //             $data[$key]['recommended']='推荐';
    //         }
    //     }
    // }
    //     $this->assign('data',$data);
    //     $this->display();


    //     if(IS_POST){
    //         $data['post_name']=I('post_name');
    //         $data['post_school']=I('post_school');
    //         $data['post_source']=I('post_source');
    //         $data['post_content']=I('post_content');
    //         $data['post_title']=I('post_title');
    //         $data['post_status']=I('post_status');
    //         $data['comment_status']=I('comment_status');
    //         $data['post_modified']=date('y-m-d h:m:s',time());
    //         $data['post_hits']=I('post_hits');
    //         $data['post_like']=I('post_like');
    //         $data['recommended']=I('recommended');
    //         $data['istop']=I('istop');
    //         $data['post_img']=I('post_img');
    //         if($this->posts_obj->where('id='.$id)->save($data))
    //             $this->success('修改成功',U('PostsAdmin/index'));
    //         else
    //             $this->error('修改失败');
    //     }
    // }

    //添加人物事迹
    public function addpost(){
        // $id = I('get.id');
        // if(isset($id) && != ''){
        //     $obj = D('posts');
        //     $result = $obj -> where("id = $id") -> find();
        // }
        // $this -> assign('result',$result);
        // echo $result;


        // $path = SITE_PATH.'data/upload/avatar/1.jpg';
        // echo $path;
        // $this->assign('path',$path);
        $this->display();
    }
}
